/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.constant.KwConstant;
import me.zhengjie.domain.KwExamPlan;
import me.zhengjie.exception.ParameterException;
import me.zhengjie.kwService.ExamPlanService;
import me.zhengjie.pojo.dto.ExamPlanFindPageDto;
import me.zhengjie.service.KwExamPlanService;
import me.zhengjie.service.dto.KwExamPlanQueryCriteria;
import me.zhengjie.util.BaseResponse;
import me.zhengjie.util.KwUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;

/**
 * @author LongYang
 * @website https://el-admin.vip
 * @date 2021-06-28
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "考试任务表前端控制器管理")
@RequestMapping("/api/kwExamPlan")
public class KwExamPlanController {

    private final KwExamPlanService kwExamPlanService;
    private final ExamPlanService examPlanService;
    private final KwUtils kwUtils;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('kwExamPlan:list')")
    public void download(HttpServletResponse response, KwExamPlanQueryCriteria criteria) throws IOException {
        String identity = kwUtils.identity();
        if (Objects.equals(identity,KwConstant.DEPT)) throw new ParameterException("无权操作");
        kwExamPlanService.download(kwExamPlanService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询考试任务表前端控制器")
    @ApiOperation("查询考试任务表前端控制器")
    @PreAuthorize("@el.check('kwExamPlan:list')")
    public ResponseEntity<Object> query(@Validated ExamPlanFindPageDto dto, Pageable pageable) {
        return new ResponseEntity<>(kwExamPlanService.queryAll(dto, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增考试任务表前端控制器")
    @ApiOperation("新增考试任务表前端控制器")
    @PreAuthorize("@el.check('kwExamPlan:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody KwExamPlan resources) throws Exception {
        boolean permission = kwUtils.isPermission(Arrays.asList(resources.getExamId()));
        if(!permission){
            throw new ParameterException("无权操作");
        }
        return new ResponseEntity<>(kwExamPlanService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改考试任务表前端控制器")
    @ApiOperation("修改考试任务表前端控制器")
    @PreAuthorize("@el.check('kwExamPlan:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody KwExamPlan resources) throws Exception {
        boolean permission = kwUtils.isPermission(Arrays.asList(resources.getExamId()));
        if(!permission){
            throw new ParameterException("无权操作");
        }
        kwExamPlanService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除考试任务表前端控制器")
    @ApiOperation("删除考试任务表前端控制器")
    @PreAuthorize("@el.check('kwExamPlan:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) throws Exception {
        kwExamPlanService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /*-----------------------------------------------------------------------------------------------*/
    @GetMapping("/findPage")
    @Log("分页查询考试任务")
    @ApiOperation("分页查询考试任务")
    @PreAuthorize("@el.check('kwExamPlan:list')")
    public BaseResponse query(@Validated ExamPlanFindPageDto dto) {
//        Set<Long> roles = kwUtils.getRoles();
//        /*获取用户的角色判断是否拥有权限*/
//        if (!roles.contains(5) || !roles.contains(1)) return new ResponseEntity<>("无权操作", HttpStatus.BAD_REQUEST);
        return BaseResponse.ok(kwExamPlanService.findPage(dto));
    }

}